/*
 * cosola.c
 *
 *  Created on: 06/12/2011
 *      Author: utn_so
 */

#include "includes/conexionAF_UNIX.h"
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv) {

    char pathSocket[100]="/home/utn_so/socket";
    char *bufferImprimir;
    uint32_t descriptor;
    uint32_t tamBufferImprimir;
    int16_t primero;
    char cadena[100];
	char *buffer;
	int32_t bytesRecividos;
	int32_t tamBuffer;
	memset(cadena,'\0',100);
	tamBufferImprimir=0;
	primero=1;

	descriptor=clienteConexionConsola(pathSocket);

	sleep(1);

	while(1){
		if(primero==1){

			bufferImprimir=NULL;
			tamBufferImprimir=175;

			bufferImprimir=calloc(1,tamBufferImprimir);

			recv(descriptor,bufferImprimir,tamBufferImprimir,0);

			printf("%s",bufferImprimir);
			primero=0;
			free(bufferImprimir);
		}
		while(fgets(cadena,100,stdin)){
			sleep(1);

			send(descriptor,cadena,100,0);
			buffer=NULL;
			bytesRecividos=0;
			tamBuffer=0;
			do{
				tamBuffer+=30;
				if(buffer==NULL){
					buffer=malloc(tamBuffer);
				}else{
					buffer=realloc(buffer,tamBuffer);
				}
				bytesRecividos=recv(descriptor,buffer,tamBuffer,MSG_PEEK);
			}while(bytesRecividos == tamBuffer);
			memset(buffer,0,tamBuffer);
			recv(descriptor,buffer,tamBuffer,0);
			printf("%s",buffer);
			if(memcmp(buffer,"gracias por usar la consola",27)==0){
				break;
			}
			free(buffer);
		}
		break;
	}

	return 0;
}
